package mediadisplay.timeline 
{
	import flash.display.GradientType;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.ByteArray;
	import mediadisplay.Constants;
	import sliderwindow.SliderWindow;
	/**
	 * ...
	 * @author ...
	 */
	public class ZoomedTimeline extends Sprite
	{
		private var _timelineContent:TimelineContent;
		public var _waveform:Sprite;
		private var _waveformData:Array;
		private var sliderWindow:SliderWindow;
		private var _cursor:Shape;
		
		public function ZoomedTimeline(timelineContent:TimelineContent) 
		{
			_timelineContent = timelineContent;
			sliderWindow = timelineContent.soundGraph.sliderWindow;
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_waveform = new Sprite();
			addChild(_waveform);
			_waveform.y = 25;
			
			var fType:String = GradientType.LINEAR;
			var colors:Array = [ 0xffffff, 0 ];
			var alphas:Array = [ 1, 1 ];
			var ratios:Array = [ 0, 255 ];
			var matr:Matrix = new Matrix();
			matr.createGradientBox( 90, 10, 0, 0, 0 );
			matr.rotate(Math.PI/2);
			var sprMethod:String = SpreadMethod.PAD;
			
			graphics.clear();
			graphics.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod );
			graphics.drawRect(0, 0, stage.stageWidth, 50);
			graphics.endFill();				
			
			_cursor = new Shape();
			addChild(_cursor);
			_cursor.graphics.lineStyle(1, 0x000000);
			_cursor.graphics.lineTo(0, height + 24);
			
			reset();
		}
		
		public function reset():void {
			_waveform.graphics.clear();
			_waveform.graphics.lineStyle(1, 0x000000, 1, true);
			_waveform.graphics.moveTo(0, 0);
			_waveform.graphics.lineTo(stage.stageWidth, 0);
			
		}
		
		public function moveCursor(currentPositionX:Number):void {
			_cursor.x = currentPositionX;
		}
		public function removeCursor():void {
			_cursor.x = 0;
		}
		public function drawWaveform(data:Array):void {
			_waveformData = data;
			if (Constants.LOGGED_IN) {
				updateWaveform();
			}
		}
		
		public function updateWaveform():void {
			
			var size:Number = sliderWindow.width;
			var start:Number = sliderWindow.x;
			
			_waveform.graphics.clear();
			_waveform.graphics.lineStyle(1, 0x000000, 1, true);
			_waveform.graphics.moveTo(0, 0);
			
			for (var i:int = 0; i < size; i++) 
			{
				var markerHeight:Number = Math.max(-25, Math.min(25, _waveformData[i + Math.floor(sliderWindow.x)]));
				var nextPointX:Number = (stage.stageWidth / size) * i;
				_waveform.graphics.moveTo(nextPointX, 0);
				//_waveform.graphics.lineTo(nextPointX, markerHeight);
				var yModPosition:Number;
				if (markerHeight < 0) {
					yModPosition -= markerHeight / 2;
				} else {
					yModPosition += markerHeight / 2;
				}
				_waveform.graphics.beginFill(0);
				_waveform.graphics.drawRect(nextPointX, -markerHeight / 2, 2, markerHeight);
				_waveform.graphics.endFill();
			}
			
			placeFocusedMarkers();
		}
		
		public function placeFocusedMarkers():void 
		{
			if (_timelineContent.movingFocusedMarker) return;
			
			var size:Number = sliderWindow.width;
			var start:Number = sliderWindow.x;
			
			var markers:Array = _timelineContent.focusedMarkers;
			for (var i:int = 0; i < markers.length; i++) 
			{
				var marker:TimelineFocusedMarker = markers[i];
				var scale:Number = marker.width / _timelineContent.overviewMarkers[i].width;
				marker.x = Math.floor(marker.xPos - (sliderWindow.x * (stage.stageWidth / sliderWindow.width)));
			}
		}
		
	}

}